/*
   Copyright 2008, Andres Postiglioni

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package com.aconcagua.samples.charts.state.model {
  import flash.events.Event;
  import flash.events.EventDispatcher;

  import mx.binding.utils.BindingUtils;
  import mx.charts.chartClasses.Series;
  import mx.charts.series.AreaSeries;
  import mx.collections.ArrayCollection;

  /**
   * @author Andres Postiglioni
   */
  public class Series extends EventDispatcher {
    private static var _idCounter: uint = 0;

    private var _id: Number;
    private var _data: ArrayCollection;

    public function Series(seriesData: ArrayCollection) {
      _id = _idCounter++;
      _data = seriesData;
    }

    model function set data(seriesData: ArrayCollection): void {
      _data = seriesData;
      this.dispatchEvent(new Event("data_changed"));
    }

/*
    model function zoom(dateFrom: Date, dateTo: Date): void {
      for each(var dataElement: SeriesElement in _data
    }
 */

    [Bindable(event="data_changed")]
    public function get length(): Number {
      return _data.length;
    }

    /**
    * @returns an ArrayCollection of data points
    **/
    [Bindable(event="data_changed")]
    public function get data(): ArrayCollection {
      return _data;
    }

    [Bindable(event="data_changed")]
    public function getElementAt(idx: int): SeriesElement {
      return _data.getItemAt(idx) as SeriesElement;
    }

    public function asFlexSeries(): mx.charts.chartClasses.Series {
      var series: AreaSeries = new AreaSeries();
      BindingUtils.bindProperty(series, "dataProvider", this, "data");
      series.xField = "date";
      series.yField = "value";

      return series;
    }
  }
}